﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace BHS.TFSChangsetAnalyser.Model
{
    public class ChangesetAnalysisResults : INotifyPropertyChanged
    {
        private readonly List<ChangesetInfo> _changesetInfos = new List<ChangesetInfo>();
        private int _numberOfChangesets;
        private int _numberOfChanges;
        private int _numberOfReviewedChanges;
        private int _percentageChangesReviewed;

        public event PropertyChangedEventHandler PropertyChanged;

        public int NumberOfChangesets
        {
            get { return _numberOfChangesets; }
            private set
            {
                _numberOfChangesets = value;
                RaisePropertyChangedEvent("NumberOfChangesets");
            }
        }

        public int NumberOfChanges
        {
            get { return _numberOfChanges; }
            private set
            {
                _numberOfChanges = value;
                RaisePropertyChangedEvent("NumberOfChanges");
            }
        }

        public int NumberOfReviewedChanges
        {
            get { return _numberOfReviewedChanges; }
            private set
            {
                _numberOfReviewedChanges = value;
                RaisePropertyChangedEvent("NumberOfReviewedChanges");
            }
        }

        public int PercentageChangesReviewed
        {
            get { return _percentageChangesReviewed; }
            private set
            {
                _percentageChangesReviewed = value;
                RaisePropertyChangedEvent("PercentageChangesReviewed");
            }
        }

        public List<ChangesetInfo> ChangesetInfos
        {
            get { return _changesetInfos; }
        }

        public bool HasResults 
        { 
            get { return ChangesetInfos.Count > 0; }
        }

       
        public void AddChangesetForAnalysis(ChangesetInfo changeset)
        {
            if (changeset == null)
            {
                throw new ArgumentNullException("changeset", "You cannot add a null changeset to the collection to be analysed");
            }

            _changesetInfos.Add(changeset);

            RecalculateStatistics(changeset);
        }

        public void ClearAnalysisResults()
        {
            _changesetInfos.Clear();
            NumberOfChanges = 0;
            NumberOfChangesets = 0;
            NumberOfReviewedChanges = 0;
            PercentageChangesReviewed = 0;
        }

        private void RecalculateStatistics(ChangesetInfo newChangeSet)
        {
            NumberOfChangesets += 1;
            NumberOfChanges += newChangeSet.NumberOfReviewableChanges;

            if (newChangeSet.WasCodeReviewed)
            {
                NumberOfReviewedChanges += newChangeSet.NumberOfReviewableChanges;
            }

            if (NumberOfChanges > 0)
            {
                PercentageChangesReviewed = Convert.ToInt32(((decimal)NumberOfReviewedChanges) / ((decimal)NumberOfChanges) * 100);
            }
        }

        private void RaisePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
